﻿using Entity;
using Entity.Mapping;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext() : base("DBConnection")
        {
            Database.SetInitializer<ApplicationDbContext>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 移除复数化表名约定
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            // 定义全局性表名规则
            modelBuilder.Types().Configure(entity => entity.ToTable(entity.ClrType.Name));
            //表映射
            EntityMap(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }

        #region Set_SystemManage
        public virtual DbSet<AreaEntity> Areas { get; set; }
        public virtual DbSet<UserEntity> Users { get; set; }
        #endregion

        /// <summary>
        /// 表映射
        /// </summary>
        /// <param name="modelBuilder"></param>
        public void EntityMap(DbModelBuilder modelBuilder)
        {
            #region Map_SystemManage
            modelBuilder.Configurations.Add(new AreaEntityMap());
            modelBuilder.Configurations.Add(new UserEntityMap());
            #endregion
        }
    }
}
